package com.boco.ibssystem.business.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boco.biinjectionserviceapi.model.RegionalLifeExpectancyManagement;
import com.boco.biinjectionserviceapi.service.RegionalLifeExpectancyManagementService;
import com.boco.ibssystem.common.exception.support.BusinessException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：
 * 人口信息配置
 *
 * @author sunshixiong
 * @date 2019/5/21-11:23
 */
@RestController
@RequestMapping(value = "/regional-life-expectancy")
public class RegionalLifeExpectancyManagementController {

  @Resource
  RegionalLifeExpectancyManagementService managementService;


  /**
   * 通过id获取
   *
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  RegionalLifeExpectancyManagement getById(@PathVariable("id") Integer id) {
    return managementService.getById(id);
  }

  /**
   * 保存实体
   *
   * @param management
   * @return
   */
  @PostMapping
  boolean save(@RequestBody RegionalLifeExpectancyManagement management) {
    Wrapper<RegionalLifeExpectancyManagement> wrapper = new QueryWrapper<RegionalLifeExpectancyManagement>().lambda()
            .eq(RegionalLifeExpectancyManagement::getYear, management.getYear());
    if (managementService.getOne(wrapper) != null) {
      throw new BusinessException(HttpStatus.BAD_REQUEST.value(), "已经有相关年度的信息");
    }
    return managementService.save(management);
  }

  /**
   * 获取列表
   *
   * @return
   */
  @GetMapping("/list")
  List<RegionalLifeExpectancyManagement> getLists() {
    return managementService.list();
  }
}
